const app = getApp()
var t = getApp().api,
  a = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hideModal: true, //模态框的状态  true-隐藏  false-显示
    animationData: {},
    store:[],
    order_list:[],
    posts_info: {
      type_id: 0,
      cart_id:0,
      title:'',
      content:'',
      order_id:0,
      order_no:'',
      pic_list:[],
      video_list: [],
      video_pic_list:[],
      address:'',
      pic_list_url:[],
      video_list_url: [],
      video_pic_list_url: [],
    },
    add_posts:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  
    getApp().page.onLoad(this, t);
    if (this.data.store.is_shenhe == 0) {
      wx.setNavigationBarTitle({
        title: '发布'
      })
    }
    var cart_id = options.id;
    this.is_power(cart_id);
    // this.setData({
    //   store:wx.getStorageSync("store")
    // })
  },

  // 判断该用户是否具有发布权限
  is_power: function(cart_id) {
    var that = this;
    a.request({
      url: t.posts.is_power,
      method: "POST",
      data: {
        cart_id: cart_id,
      },
      success: function(t) {
        wx.hideLoading()
        if (t.code == 0) {
          that.data.posts_info.type_id = t.posts_type;
          that.data.posts_info.cart_id = cart_id;
          that.setData({
            posts_info: that.data.posts_info
          })
          return true;
        } else {
          wx.showModal({
            title: "提示",
            content: t.msg,
            showCancel: !1,
            success: function(t) {
              t.confirm && wx.navigateTo({
                url: "/posts/posts-list/index"
              });
            }
          });
        }
      }
    })
  },
  showModal: function() {
    var that = this;
    that.setData({
      hideModal: false
    })
    var animation = wx.createAnimation({
      duration: 200, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
      timingFunction: 'linear', //动画的效果 默认值是linear
    })
    this.animation = animation
    that.fadeIn(); //调用显示动画
  },

  // 隐藏遮罩层
  hideModal: function() {
    var that = this;
    var animation = wx.createAnimation({
      duration: 200, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
      timingFunction: 'linear', //动画的效果 默认值是linear
    })
    this.animation = animation
    that.fadeDown(); //调用隐藏动画   
    setTimeout(function() {
      that.setData({
        hideModal: true
      })
    }, 200) //先执行下滑动画，再隐藏模块

  },

  //动画集
  fadeIn: function() {
    this.animation.translateY(0).step()
    this.setData({
      animationData: this.animation.export() //动画实例的export方法导出动画数据传递给组件的animation属性
    })
  },
  fadeDown: function() {
    this.animation.translateY(300).step()
    this.setData({
      animationData: this.animation.export(),
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  // 文件类型
  add_posts: function () {
    var that = this;
    this.setData({
      add_posts: !that.data.add_posts
    })
  },

  chooseVideo: function (t) {
    var e = this,
      i = e.data.posts_info,
      o = i.video_list.length;
      if (o >= 1 || i.pic_list.length>0){
          wx.showToast({
            icon: 'none',
            title: '只能上传一个视频，或九张图片'
          })
          return;
        }
        wx.chooseVideo({
          count: 1,
          success: function (t) {
            console.log(t);
            e.add_posts()
            i.video_pic_list = i.video_pic_list.concat(t.thumbTempFilePath),i.video_list = i.video_list.concat(t.tempFilePath), e.setData({
              posts_info: i
            });
          }
        });
  },

  chooseImage: function(t) {
    var e = this,
      i = e.data.posts_info,
      o = i.pic_list.length,
      v = i.video_list.length;
      if (o >= 9 || v >= 1) {
        wx.showToast({
          icon: 'none',
          title: '只能上传一个视频，或九张图片'
        })
        return;
      }
    wx.chooseImage({
      count: 9 - o,
      success: function(t) {
        e.add_posts()
        i.pic_list = i.pic_list.concat(t.tempFilePaths), e.setData({
          posts_info: i
        });
      }
    });
  },
  deleteImage: function(t) {
    var e = this,
      i = t.currentTarget.dataset.picIndex,
      o = e.data.posts_info;
    o.pic_list.splice(i, 1), e.setData({
      posts_info: o
    });
  }, 

  deleteVideo: function(t) {
    var e = this,
      i = t.currentTarget.dataset.picIndex,
      o = e.data.posts_info;
    o.video_list.splice(i, 1), o.video_pic_list.splice(i, 1), e.setData({
      posts_info: o
    });
  }, 

  commentSubmit: function (a) {
    function i(e) {
      if (e == n.length) return o();
      var ii =0;
      for (var s in n) !function (oo) {
        wx.uploadFile({
          url: t.default.upload_image,
          name: "image",
          formData: r,
          filePath: n[oo],
          complete: function (t) {
            if (t.data) {
              var s = JSON.parse(t.data);
              if (0 == s.code && (posts_info.pic_list_url[oo] = s.data.url)){
                that.setData({
                  posts_info: posts_info
                })
                ii++;
              }
            }
            if (ii==n.length) return o();
          }
        });
      }(s);
    }
    // 上传视频缩略图
    function video_pic(e) {
      if (e == vid_pic.length) return vv(0);
      var ii = 0;
      for (var s in vid_pic) !function (oo) {
        if (!vid_pic[oo]) return vv(0);
        wx.uploadFile({
          url: t.default.upload_image,
          name: "image",
          formData: r,
          filePath: vid_pic[oo],
          complete: function (t) {
            if (t.data) {
              var s = JSON.parse(t.data);
              if (0 == s.code && (posts_info.video_pic_list_url[oo] = s.data.url)) {
                that.setData({
                  posts_info: posts_info
                })
                ii++;
              }
            }
            if (ii == vid_pic.length) return vv(0);
          }
        });
      }(s);
    }


    // 上传视频
    function vv(e) {
      if (e == vid.length) return o();
      var ii = 0;
      for (var s in vid) !function (oo) {
        wx.uploadFile({
          url: t.default.upload_video,
          name: "video",
          formData: r,
          filePath: vid[oo],
          complete: function (tt) {
            if (tt.data) {
              var ss = JSON.parse(tt.data);
              if (0 == ss.code && (posts_info.video_list_url[oo] = ss.data.url)) {
                that.setData({
                  posts_info: posts_info
                })
                ii++;
              }
            }
            if (ii == vid.length) return o();
          }
        });
      }(s);
    }

    function o() {
      // posts_info.content = escape(posts_info.content);
      // posts_info.title = escape(posts_info.title);
      getApp().request({
        url: t.posts.add_posts,
        method: "post",
        data: s.data.posts_info,
        success: function (t) {
          wx.hideLoading(), wx.showModal({
            title: "提示",
            content: t.msg,
            showCancel: !1,
            success: function (e) {
              e.confirm && wx.navigateTo({
                url: "/posts/posts/index"
              })
            },
            fail: function (t) {
              wx.showModal({
                title: "提示",
                content: "上传内容不能包含表情，特殊字符",
                showCancel: !1,
                success: function (e) {
                  
                }
              })
            }
          })
        }
      });
    }
    var s = this;
    var that = this;
    var n = s.data.posts_info.pic_list, vid = s.data.posts_info.video_list, vid_pic = s.data.posts_info.video_pic_list ,posts_info = s.data.posts_info, r = {};
    console.log(vid_pic)
    if (posts_info.content == "" && posts_info.pic_list.length < 1 && posts_info.video_list.length < 1) {
      wx.showModal({
        title: "提示",
        content: "请填写内容或上传图片或视频",
        showCancel: !1,
        success: function (e) {
          
        }
      })
      return false;
    } 
    if (posts_info.type_id == 1 && posts_info.order_id == 0) {
      wx.showModal({
        title: "提示",
        content: "请选择订单",
        showCancel: !1,
        success: function (e) {

        }
      })
      return false;
    }
    wx.showLoading({
      title: "正在提交",
      mask: !0
    });
    if(vid.length > 0){
      video_pic(0)
    }else{
      i(0);
    }
    
  },
  bindContentInput: function (t) {
    this.data.posts_info.content = t.detail.value, this.setData({
      posts_info: this.data.posts_info
    });
  },
  bindTitleInput: function (t) {
    this.data.posts_info.title = t.detail.value, this.setData({
      posts_info: this.data.posts_info
    });
  },

  // 选择所晒订单
  get_order:function(){
    var that = this;
    wx.showLoading({
      title: "加载中",
      mask: !0
    });
    getApp().request({
      url: t.posts.get_order,
      method: "post",
      success: function (t) {
        wx.hideLoading()
        if(t.code==0){
          if(t.data.length>0){
            that.showModal()
            that.setData({
              order_list : t.data
            })
          }else{
            wx.showModal({
              title: "提示",
              content: "暂无评价订单",
              showCancel: !1,
              success: function (e) {
              }
            })
            return false;
          }
        }else{
          wx.showModal({
            title: "提示",
            content: "暂无评价订单",
            showCancel: !1,
            success: function (e) {

            }
          })
          return false;
        }
      }
    });
  },
  add_order_id:function(tt){
    var order_id = tt.currentTarget.dataset.id;
    var order_no = tt.currentTarget.dataset.no;
    var that = this;
    that.data.posts_info.order_id = order_id;
    that.data.posts_info.order_no = order_no;
    that.setData({
      posts_info:that.data.posts_info
    })
    this.hideModal();
  },
  dingwei: function () {
    var t = this;
    wx.chooseLocation({
      success: function (e) {
        t.data.posts_info.address = e.address
        t.setData({
          posts_info: t.data.posts_info
        })
      },
      fail: function (a) {
        a.getauth({
          content: "需要获取您的地理位置授权，请到小程序设置中打开授权",
          success: function (e) {
            e && (e.authSetting["scope.userLocation"] ? t.dingwei() : wx.showToast({
              title: "您取消了授权",
              image: "/images/icon-warning.png"
            }));
          }
        });
      },
      complete: function () {
        
      }
    });
  },

})